package com.chen.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.dto.CourseDTO;
import com.chen.entity.Course;
import com.chen.mapper.CourseMapper;
import com.chen.mapper.ExamPlanMapper;
import com.chen.mapper.SubjectMapper;
import com.chen.service.CourseService;
import com.chen.vo.CourseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 科目相关服务实现
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    // 考试计划
    @Autowired
    private ExamPlanMapper examPlanMapper;

    // 试题
    @Autowired
    private SubjectMapper subjectMapper;

    /**
     * 根据分科情况查看科目信息
     * @return
     */
    @Override
    public List<CourseVO> listCourses(Integer gradeId, Integer division) {
        return baseMapper.findCourseByGradeIdAndDivision(gradeId, division);
    }

    /**
     * 查询全部科目
     * @return
     */
    @Override
    public List<CourseVO> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public CourseVO getCourseById(Integer courseId) {
        Assert.isTrue(ObjectUtil.isNotEmpty(courseId), "学科id不能为空");
        return baseMapper.getCourseById(courseId);
    }

    /**
     * 新增科目信息
     * @param dto
     * @return
     */
    @Override
    public Integer saveCourse(CourseDTO dto) {
        // 学科名
        String name = dto.getName();
        Assert.isTrue(ObjectUtil.isNotEmpty(name), "学科名不能为空");
        // 分科情况
        Integer division = dto.getDivision();
        Assert.isTrue(ObjectUtil.isNotEmpty(division), "分科情况不能为空");
        // 年级
        Integer gradeId = dto.getGradeId();
        Assert.isTrue(ObjectUtil.isNotEmpty(gradeId), "年级不能为空");
        Course course = Course.builder().name(name).division(division).gradeId(gradeId).build();
        return baseMapper.insert(course);
    }

    /**
     * 修改科目信息
     * @param dto
     * @return
     */
    @Override
    public Integer updateCourse(CourseDTO dto) {
        Integer id = dto.getId();
        Assert.isTrue(ObjectUtil.isNotEmpty(id), "学科id不能为空");
        // 学科名
        String name = dto.getName();
        Assert.isTrue(ObjectUtil.isNotEmpty(name), "学科名不能为空");
        // 分科情况
        Integer division = dto.getDivision();
        Assert.isTrue(ObjectUtil.isNotEmpty(division), "分科情况不能为空");
        // 年级
        Integer gradeId = dto.getGradeId();
        Assert.isTrue(ObjectUtil.isNotEmpty(gradeId), "年级不能为空");
        Course course = Course.builder().id(id).name(name).division(division).gradeId(gradeId).build();
        return baseMapper.updateById(course);
    }

    @Override
    public Integer delCourse(Integer courseId) {
        Assert.isTrue(ObjectUtil.isNotEmpty(courseId), "学科id不能为空");
        // 考试计划个数
        Long countByCourseId = examPlanMapper.findCountByCourseId(courseId);
        // 试题个数
        Long subjectCount = subjectMapper.findCountByCourseId(courseId);
        if (countByCourseId == 0 && subjectCount == 0) {
            return baseMapper.deleteById(courseId);
        }
        return 0;
    }
}
